var setId;
var mode = 0; //0-管理  1-添加

var templateParams = {
    tableTheads:["用例分类", "用例编号", "用例名称", "测试步骤","用例类型", "执行顺序", "创建时间", "创建人","备注","操作"],
    btnTools:[{
        type:"primary",
        size:"M",
        id:"manger-case",
        iconFont:"&#xe60c;",
        name:"管理用例"
    },{
        size:"M",
        id:"add-case",
        iconFont:"&#xe600;",
        name:"添加用例"
    },{
        size:"M",
        id:"batch-op",
        iMarkClass:"Hui-iconfont-del3",
        name:"批量操作"
    },{
        type:"danger",
        size:"M",
        id:"setting-case-order",
        iconFont:"&#xe6f3;",
        name:"用例排序"
    }]
};

var columnsSetting = [
    {
        "data":null,
        "width":"25",
        "render":function(data, type, full, meta){
            return checkboxHmtl(data.case_title, data.id, "selectCase");
        }},
    {
        "data":"id",
        "width":"55",
        "render":function(data, type, full, meta){
            return full.case_id || data;
        }
    },
    {
        "data":"classed_path",
        "render":function(data, type, full, meta){
            return full.case_id || data;
        }
    },
    ellipsisData("case_num"),
    {
        "data":"case_title",
        "width":"18%",
    },
    {
        "data":"step_count",
        "width":"55",
        "render":function(data, type, full, meta){
            var context =
                [{
                    type:"default",
                    size:"M",
                    markClass:"show-case-step",
                    name:data || 0
                }];
            return btnTextTemplate(context);
        }
    },
    {"data":"case_type", "width":"55"},
    {
        "data": "seq_num",
        "width":"50",
        "render":function(data) {
            return data || '';
        }
    },
    ellipsisData("create_time"),
    ellipsisData("creator_name"),
    longTextData("mark", "case_title", "备注信息"),
    {
        "data":null,
        "width":"30",
        "render":function(data, type, full, meta){
            var context;
            //管理
            if (mode == 0) {
                context = [{
                    title:"删除",
                    markClass:"del-set-case",
                    iconFont:"&#xe6e2;"
                }];
            }

            //添加
            if (mode == 1) {
                context = [{
                    title:"添加",
                    markClass:"add-set-case",
                    iconFont:"&#xe600;"
                }];
            }
            return btnIconTemplate(context);
        }}
];


var eventList = {
    //展示测试步骤
    ".show-case-step":function() {
        let data = table.row( $(this).parents('tr') ).data();
        parent.layer_show(data.case_title + '-测试步骤', 'webCaseStep.html?case_id=' + data.id
            , '86%', '90%', 2
            , function(layero, index){
            }, null, function(){
            refreshTable();
        });
    },
    "#batch-op":function() { //批量操作 删除或者添加
        let checkboxList = $(".selectCase:checked");
        if (checkboxList.length < 1) {
            return false;
        }

        let opName = "删除";
        let url = REQUEST_URL.WEB_CASE_SET.delCase2Set;
        let sendData = [];
        if (mode == 1) {
            opName = "添加";
            url = REQUEST_URL.WEB_CASE_SET.addCase2Set + '&set_id=' + setId;
        }

        $.each(checkboxList, function(i, n){
            let data = table.row( $(n).parents('tr') ).data();
            sendData.push(mode == 1 ? data.id : data.map_id);
        });

        layer.confirm('确定' + opName + '选中的' + checkboxList.length + '项吗？', {icon: 0, title: '提示'}, function(index){
            layer.close(index);
            GLOBAL_UTILS.ajaxUtils.ajaxPost(url, sendData, function(data){
                if (mode == 0) {
                    refreshTable();
                }
                layer.msg(opName + '成功！', {icon: 1, time: 1600});
            });
        });
    },
    "#manger-case":function() { //管理测试集 - 从测试集中删除
        var that = this;
        mode = 0;
        refreshTable(REQUEST_URL.WEB_CASE_SET.listSetCase + "&set_id=" + setId, function(json) {
            $(that).addClass('btn-primary').siblings("#add-case").removeClass('btn-primary');
        }, null, true);
        $("#batch-op").children("i").removeClass("Hui-iconfont-add").addClass("Hui-iconfont-del3");
    },
    "#add-case":function() { //添加测试用例
        var that = this;
        mode = 1;
        refreshTable(REQUEST_URL.WEB_CASE.listCase + "?set_id=" + setId, function(json) {
            $(that).addClass('btn-primary').siblings("#manger-case").removeClass('btn-primary');
        }, null, true);
        $("#batch-op").children("i").removeClass("Hui-iconfont-del3").addClass("Hui-iconfont-add");
    },
    //从测试集中删除
    ".del-set-case":function(){
        var data = table.row( $(this).parents('tr') ).data();
        var that = this;
        layer.confirm('确定要从测试集中删除此用例吗?', {icon:0, title:'警告'}, function(index) {
            layer.close(index);

            GLOBAL_UTILS.ajaxUtils.ajaxPost(REQUEST_URL.WEB_CASE_SET.delCase2Set, [data.map_id], function(data){
                table.row($(that).parents('tr')).remove().draw();
                layer.msg('删除成功!', {icon:1, time:1500});
            });
        });
    },
    //添加到测试集
    ".add-set-case": function() {
        var data = table.row( $(this).parents('tr') ).data();
        layer.confirm('确定添加此用例到测试集中吗?', {icon:0, title:'提示'}, function(index) {
            layer.close(index);

            GLOBAL_UTILS.ajaxUtils.ajaxPost(REQUEST_URL.WEB_CASE_SET.addCase2Set + '&set_id=' + setId, [data.id] ,function(data){
                layer.msg('添加成功!', {icon:1, time:1500});
            });
        });
    },
    //用例排序
    "#setting-case-order": function() {
        GLOBAL_UTILS.ajaxUtils.ajaxGet(REQUEST_URL.WEB_CASE_SET.listSetCase + "&set_id=" + setId, function (data) {
            if (data.data.length < 2) {
               layer.msg('测试用例太少了,再添加几个吧!', {time:1500});
               return false;
            }
            let heignt = 140 + 40 * data.data.length;
            let sortableHtml = templates["sort-set-cases"](data.data);

            layer_show("用例执行排序-请手动拖拽", sortableHtml, 680, heignt, 1, function (layero, index) {
                $(layero).spinModal();
                var sortObj = new Sortable(document.getElementById('set-cases'), {
                    animation: 150
                });
                var orderArr = [];
                for (var i = 1;i <= data.data.length;i++){
                    orderArr.push(data.data[i - 1]['map_id'] + "");
                }
                sortObj.sort(orderArr);
                $("#save-set-cases-sort").click(function () {//保存提交到后台并关闭弹出层
                    if (JSON.stringify(sortObj.toArray()) == JSON.stringify(orderArr)) {//没有改动
                        layer.msg('未作改动!', {time:1500});
                        layer.close(index);
                        return false;
                    }

                    GLOBAL_UTILS.ajaxUtils.ajaxPost(REQUEST_URL.WEB_CASE_SET.sortSetCase, sortObj.toArray(), function(json){
                        layer.msg("保存排序成功!", {icon:1, time:1800})
                        refreshTable();
                        layer.close(index);
                    });
                });
                $("#reset-set-cases-sort").click(function () {//重置
                    sortObj.sort(orderArr);
                });
                $(layero).spinModal(false);
            }, null, function () {
                $("#save-set-cases-sort").unbind('click');
                $("#reset-set-cases-sort").unbind('click');
            }, {
                //skin: 'layui-layer-rim', //加上边框
                shade: 0
            });
        });
    }
};


var mySetting = {
    eventList:eventList,
    templateCallBack:function(df){
        setId = GetQueryString("set_id");
        publish.renderParams.listPage.listUrl = REQUEST_URL.WEB_CASE_SET.listSetCase + "&set_id=" + setId;

        df.resolve();
    },
    listPage:{
        tableObj:".table-sort",
        columnsSetting:columnsSetting,
        columnsJson:[0, 10],
        dtOtherSetting:{
            serverSide:false
        },
        exportExcel:false
    },
    templateParams:templateParams
};


$(function(){
    publish.renderParams = $.extend(true,publish.renderParams,mySetting);
    publish.init();
});
